<template>
  <div class="app-container">
    <el-card style="border-radius: 10px">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
          <el-form-item label="订单编号" prop="orderCode">
            <el-input
              v-model="queryParams.orderCode"
              placeholder="请输入订单编号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="计划编号" prop="planNumber">
            <el-input
              v-model="queryParams.planNumber"
              placeholder="请输入计划编号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="供应商名称" prop="vendorName">
            <el-input
              v-model="queryParams.vendorName"
              placeholder="请输入供应商名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="订单状态" prop="status">
            <el-select v-model="queryParams.status" placeholder="请选择订单状态" clearable>
              <el-option label="已审批" value="0"/>
              <el-option label="待确认" value="3"/>
              <el-option label="待审批" value="4"/>
              <el-option label="已驳回" value="5"/>
            </el-select>
          </el-form-item>
          <el-form-item label="供货日期" prop="params">
            <el-date-picker
              clearable
              v-model="queryParams.params"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="yyyy-MM-dd"
              placeholder="请选择供货日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
    </el-form>
    </el-card>
    <div style="height: 15px"></div>
    <el-card style="border-radius: 10px">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mes:cg:order:add']"
        >新增
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column label="订单编号" align="center" prop="orderCode" width="190">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleInfo(scope.row)"
            v-hasPermi="['mes:cg:order:query']"
          >{{scope.row.orderCode}}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="供货日期" align="center" prop="materialDeadline">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.materialDeadline, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="供应商名称" width="160" align="center" prop="vendorName" :show-overflow-tooltip="true"/>
      <el-table-column label="联系人" align="center" prop="contact"/>
      <el-table-column label="电话" align="center" prop="contactTel"/>
      <el-table-column label="成本" width="120" align="center" prop="estimatedCost">
        <template slot-scope="scope">
          <el-tag type="danger">{{ formatCurrency(scope.row.estimatedCost) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag type="success" v-show="scope.row.status==0&&scope.row.approvalStatus==scope.row.totalStep" dffect="dark">已审批</el-tag>
          <el-tag type="" v-show="scope.row.status==1&&scope.row.approvalStatus==scope.row.totalStep" dffect="dark">采购已申请入库</el-tag>
          <el-tag type="success" v-show="scope.row.status==2&&scope.row.approvalStatus==scope.row.totalStep" dffect="dark">确认入库</el-tag>
          <el-tag type="info" v-show="scope.row.status==3&&scope.row.approvalStatus==null" dffect="dark">待确认</el-tag>
          <el-tag type="primary" v-show="scope.row.status==4&&scope.row.approvalStatus==null" dffect="dark">待审批</el-tag>
          <el-tag type="danger" v-show="scope.row.status==5&&scope.row.approvalStatus==null" dffect="dark">已驳回</el-tag>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" align="center" width="250">
        <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-s-check"
              @click="handleView(scope.row)"
              v-show="(scope.row.status === 3 || scope.row.status === 5)&&scope.row.approvalStatus==null||scope.row.approvalStatus<0"
              v-hasPermi="['mes:cg:order:confirm']"
            >确认并提交审批
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-show="(scope.row.status === 3 || scope.row.status === 5)&&scope.row.approvalStatus==null||scope.row.approvalStatus<0"
              v-hasPermi="['mes:cg:order:edit']"
            >修改
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-show="(scope.row.status === 3 || scope.row.status === 5)&&scope.row.approvalStatus==null||scope.row.approvalStatus<0"
              v-hasPermi="['mes:cg:order:remove']"
            >删除
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-upload2"
              @click="handleUploadDrawing(scope.row)"
              v-show="scope.row.status==0&&scope.row.approvalStatus==scope.row.totalStep"
              v-hasPermi="['mes:cg:order:uploadDrawing']"
            >上传图纸
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-switch-button"
              @click="handleTerminationOrder(scope.row)"
              v-show="scope.row.status==0&&scope.row.approvalStatus==scope.row.totalStep"
              v-hasPermi="['mes:cg:order:termination']"
            >终止
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-video-play"
              @click="handleContinueOrder(scope.row)"
              v-show="scope.row.status==6"
              v-hasPermi="['mes:cg:order:continue']"
            >继续执行
            </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    </el-card>
    <InfoView ref="infoView"></InfoView>
    <AddOrder ref="add"></AddOrder>
    <OrderDrawing ref="drawing"></OrderDrawing>
  </div>
</template>

<script>
import {listOrder,delOrder,terminationOrder,continueOrder } from "@/api/mes/cg/order";
import InfoView from "@/views/mes/cg/order/infoView.vue"
import AddOrder from "@/views/mes/cg/order/add.vue"
import OrderDrawing from "@/views/mes/cg/order/drawing.vue"

export default {
  name: "Order",
  components: {
    InfoView,
    AddOrder,
    OrderDrawing
  },
  dicts: ['erp_payment_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 采购订单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderCode: null,
        planNumber: null,
        materialDeadline: null,
        vendorName: null,
        paymentType: null,
        status: null,
        params: [],
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询采购订单列表 */
    getList() {
      this.loading = true;
      const query = { ...this.queryParams };
      if (query.params && query.params.length === 2) {
        query.params = {
          beginTime: query.params[0],
          endTime: query.params[1]
        };
      } else {
        query.params = null;
      }
      listOrder(query).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderCode: null,
        planId: null,
        planNumber: null,
        planName: null,
        materialDeadline: null,
        vendorId: null,
        vendorCode: null,
        vendorName: null,
        contact: null,
        contactTel: null,
        paymentType: null,
        status: null,
        remarks: null,
        estimatedCost: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.cgPurchaseOrderDetailsList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.add.open=true;
      this.$refs.add.autoGenFlag=false;
      this.$refs.add.title='新增采购订单';
      this.$refs.add.optType='add';
      this.$refs.add.addGoodsList=[];
      this.$refs.add.init();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.add.open=true;
      this.$refs.add.autoGenFlag=false;
      this.$refs.add.title='修改采购订单';
      this.$refs.add.optType='edit';
      this.$refs.add.update(row.id);
    },
    /** 确认按钮操作 */
    handleView(row) {
      this.$refs.infoView.open = true;
      this.$refs.infoView.optType = 'confirm';
      this.$refs.infoView.title = '确认采购订单';
      this.$refs.infoView.init(row.id);
    },
    /** 上传图纸按钮操作 */
    handleUploadDrawing(row) {
      this.$refs.drawing.open = true;
      this.$refs.drawing.title = '确认采购订单';
      this.$refs.drawing.init(row.id);
    },
    //终止订单
    handleTerminationOrder(row) {
      this.$modal.confirm('是否确认终止编号为"' + row.orderCode + '"的订单？').then(function () {
        return terminationOrder(row.orderCode);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("订单已终止");
      }).catch(() => {
      });
    },
    //继续执行订单
    handleContinueOrder(row) {
      this.$modal.confirm('是否确认继续执行编号为"' + row.orderCode + '"的订单？').then(function () {
        return continueOrder(row.orderCode);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("订单已继续执行");
      }).catch(() => {
      });
    },
    /** 查看按钮操作 */
    handleInfo(row) {
      this.$refs.infoView.open = true;
      this.$refs.infoView.optType = 'view';
      this.$refs.infoView.title = '查看采购订单';
      this.$refs.infoView.init(row.id);
    },
    /** 申请入库按钮操作 */
    handleWarehousing(row) {
      this.$refs.infoView.open = true;
      this.$refs.infoView.optType = "warehousing";
      this.$refs.infoView.title = '采购订单-入库单';
      this.$refs.infoView.init(row.id);
    },
    handleDelete(row) {
      const orderIds = row.id || this.ids;
      this.$modal.confirm('是否确认删除编号为"' + row.orderCode + '"订单？').then(function () {
        return delOrder(orderIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('mes/cg/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    },
    formatCurrency(number) {
      return new Intl.NumberFormat('zh-CN', { style: 'currency', currency: 'CNY' }).format(number);
    },
  }
};
</script>
